package de.timoklostermann.heartbeat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    private long startTime = 0;
    private boolean measurementStarted = false;
    private int beats = 0;
    
    
    private TextView tv_bpm;
    private TextView tv_time;
    private TextView tv_touch;
    
    private Vibrator vib;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final CountDownTimer ct = new CountDownTimer(3000,100) {
			@Override
			public void onTick(long millisUntilFinished) {
				MainActivity.this.update();
			}
			
			@Override
			public void onFinish() {
				MainActivity.this.update();
				tv_touch.setText(R.string.main_touch);
				measurementStarted = false;
				beats = 0;
			}
		};
		
        tv_bpm = (TextView) this.findViewById(R.id.main_bpm);
        tv_time = (TextView) this.findViewById(R.id.main_seconds);
        tv_touch = (TextView) this.findViewById(R.id.main_touch);
        
        vib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        
        tv_touch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
				int vibration = Integer.parseInt(sharedPrefs.getString("vib_pref", "100"));
				vib.vibrate(vibration);
				
				if(!measurementStarted) {
					measurementStarted = true;
					startTime = System.currentTimeMillis();
					tv_touch.setText("");
					tv_time.setVisibility(View.VISIBLE);
				}
				beats++;
				
				ct.cancel();
				ct.start();
				
				updateBPM(System.currentTimeMillis());
			}
			
		});        
    }

    public void update() {
    	long time = System.currentTimeMillis() - startTime;
    	this.updateFontColor(time);
    	this.updateTime(time);
    }
    
    private void updateFontColor(long time) {
    	int r = 255;
    	int g = 0;
    	if(time > 0 && time < 15000) { // red
    		g = g + (int)((time/1000)+1)*17;
    	}
    	else if(time > 15000 && time < 30000) { // yellow
    		g = 255;
    		r = r - (int)((time/1000)+1)*17;
    	}
    	else { // green
    		r = 0;
    		g = 255;
    	}
    	
    	tv_bpm.setTextColor(Color.rgb(r, g, 0));
    }
    
    private void updateBPM(long time) {
    	long difference = time - startTime;
    	if(difference > 0) {
    		int bpm = (int) Math.round(beats * (60d*1000d)/difference);
    		if(bpm >= 1000) {
        		tv_bpm.setText("-- BPM");
        	} else {
        		tv_bpm.setText(bpm + " BPM");
        	}
    	}
    }
    
    private void updateTime(long time) {
    	tv_time.setText(time/1000 + "," + (time%1000)/100 + " Sekunden");
    }
    
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
		switch(item.getItemId()) {
			case R.id.menu_info:
				// Info Activity
				intent = new Intent(MainActivity.this, InfoActivity.class);
				startActivity(intent);
				break;
			case R.id.menu_about:
				// About Activity
				intent = new Intent(MainActivity.this, AboutActivity.class);
				startActivity(intent);
				break;
			case R.id.menu_settings:
				// Settings
				intent = new Intent(MainActivity.this, Preferences.class);
				startActivity(intent);
				break;
			default:
				return super.onOptionsItemSelected(item);
		}
    	
    	return false;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
